﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Mammoth.Entities;
using Mammoth.Events;
using Mammoth.Factories;
using Mammoth.Navigations;
using Mammoth.Services;
using Mammoth.ViewModels;
using Mammoth.Views;
using Mammoth.Views.Account;

namespace Mammoth.ViewModels
{
    public class AccountListViewModel : WPFViewModelBase
    {
        public static readonly RoutedCommand LoginCommand = new RoutedCommand();
        public static readonly RoutedCommand RemoveCommand = new RoutedCommand();
        public static readonly RoutedCommand EditCommand = new RoutedCommand();

        public IList<AccountEntity> AccountList { get; private set; }
        public IGTalkService GTalkService { get; private set; }

        public AccountListViewModel(IInputElement dispatcher, ITypeFactory viewFactory, IGTalkService service)
            : base(dispatcher)
        {
            GTalkService = service;
            GTalkService.AccountListChange += GTalkService_AccountListChange;
            AccountList = new ObservableCollection<AccountEntity>(GTalkService.AccountList);
        }

        ~AccountListViewModel()
        {
            GTalkService.AccountListChange -= GTalkService_AccountListChange;
        }

        void GTalkService_AccountListChange(object sender, EventArgs e)
        {
            AccountList = new ObservableCollection<AccountEntity>(GTalkService.AccountList);
        }

        public void LoginHandler(object sender, ExecutedRoutedEventArgs e)
        {
            try
            {
                AccountEntity acc = (AccountEntity)e.Parameter;
                if (acc.State == AccountState.Login)
                    Logout(acc.Email);
                else
                    Login(acc.Email, acc.Password);
            }
            catch (Exception ex)
            {
                Dispatcher.RaiseErrorEvent(ex.Message);
            }
        }

        public void EditAccountHandler(object sender, ExecutedRoutedEventArgs e)
        {
            AccountEntity acc = (AccountEntity)e.Parameter;
            EditAccount(acc.Email);
        }

        public void RemoveAccountHandler(object sender, ExecutedRoutedEventArgs e)
        {
            try
            {
                AccountEntity acc = (AccountEntity)e.Parameter;
                var msg = string.Format("Anda yakin akan menghapus {0}", acc.Email);
                var ev = new MessageBoxEventArgs(msg, "Konfirmasi", MessageBoxButton.YesNo);
                RaiseEvent(ev);
                if (ev.Result == MessageBoxResult.Yes)
                {
                    RemoveAccount(acc.Email);
                }
            }
            catch (Exception ex)
            {
                Dispatcher.RaiseErrorEvent(ex.Message);
            }
        }

        public void ShowAddAccountForm()
        {
            Dispatcher.RaiseNavigationEvent(NavigationType.Show, typeof(AddAccountForm));
        }

        public void RemoveAccount(string email)
        {
            Logout(email);
            GTalkService.RemoveAccount(email);
        }

        public void EditAccount(string email)
        {
            Dispatcher.RaiseNavigationEvent(NavigationType.Show, typeof(AddAccountForm),
                new AddAccountArguments() { Email = email });
        }


        public void Login(string email, string password)
        {
            GTalkService.DoLogin(email, password);
        }

        public void Logout(string email)
        {
            GTalkService.DoLogout(email);
        }

    }
}
